import { ALL_CATEGORY_ITEM, CATEGORY_NOMAR_DATA } from "@/constants/index.js";
import { getCategories } from "@/api/categories/index.js";

export default {
  namespaced: true,
  state: () => ({
    // vuex持久化，会先读取缓存数据，如果没有则读取默认数据
    categorys: CATEGORY_NOMAR_DATA,
  }),
  mutations: {
    setCategorys(state, newCategorys) {
      state.categorys = [ALL_CATEGORY_ITEM, ...newCategorys];
    },
  },
  actions: {
    async useCategoryData(context) {
      const { categorys } = await getCategories();
      context.commit("setCategorys", categorys);
    },
  },
};
